package com.awakensoftware.hunterserver.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.awakensoftware.hunterserver.exceptions.entityexceptions.EntityConstraintViolationException;
import com.awakensoftware.hunterserver.exceptions.entityexceptions.EntityException;
import com.awakensoftware.hunterserver.serverentities.Chest;
import com.awakensoftware.hunterserver.services.ChestService;
import com.awakensoftware.hunterserver.services.HunterService;
import com.awakensoftware.hunterserver.services.TreasureService;
import com.awakensoftware.hunterserver.utilities.geometry.LengthUnit;

@Controller
@RequestMapping("/tests/*")
@Transactional
public class TestController {

	@Autowired
	private ChestService chestService;

	@Autowired
	private TreasureService treasureService;

	@Autowired
	private HunterService hunterService;

	@ResponseBody
	@RequestMapping(value = "/1")
	public String createNewChest() {
		Chest chest = null;
		try {
			chest = chestService.create("haha");
		} catch (EntityException e) {
			return e.getMessage();
		}

		try {
			chestService.addTreasureToChest(chest, treasureService.create());
		} catch (EntityException e) {
			return e.getMessage();
		}
		return "OK";
	}

	@ResponseBody
	@RequestMapping(value = "/1b")
	public String createNewChestWithCoordinate() {
		Chest chest = null;
		try {
			chest = chestService.create("VancouverChest", -123.1867656, 49.2637339);
		} catch (EntityException e) {
			return e.getMessage();
		}

		try {
			chestService.addTreasureToChest(chest, treasureService.create());
		} catch (EntityException e) {
			return e.getMessage();
		}
		return "OK";
	}
	
	@RequestMapping(value = "/1c")
	public String testJsp() {
		return "Test";
	}

	@ResponseBody
	@RequestMapping(value = "/2")
	public String getNumberOfChests() {
		return "" + chestService.search("haha").size();
	}

	@ResponseBody
	@RequestMapping(value = "/2b")
	public String getNumberOfChestsVancouver() {
		return "" + chestService.search("VancouverChest").size();
	}

	@ResponseBody
	@RequestMapping(value = "/2c")
	public String getNumberOfChestsNearVancouverLocation() {
		return "" + chestService.searchNearbyChests(-123.1867656, 50, 100000, LengthUnit.METER).size();
	}

	@ResponseBody
	@RequestMapping(value = "/2d")
	public String getZeroNumberrOfChestsNearVancouverLocation() {
		return "" + chestService.searchNearbyChests(-123.1867656, 51, 100000, LengthUnit.METER).size();
	}

	@ResponseBody
	@RequestMapping(value = "/3")
	public String createNewHunter() {
		try {
			hunterService.create("username");
		} catch (EntityConstraintViolationException e) {
			return "Hunter with same username already exists " + e.getMessage();
		} catch (EntityException e) {
			return e.getMessage();
		}
		return "OK";

	}

}
